package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.ProTask;
import org.example.erp.domain.ProWorkorder;
import org.example.erp.domain.ProcedureDetails;
import org.example.erp.service.ProTaskService;
import org.example.erp.service.ProWorkorderService;
import org.example.erp.service.ProceduresDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ProTaskController {
    @Autowired
    ProTaskService proTaskService;
    @Autowired
    ProWorkorderService proWorkorderService;
    @Autowired
    ProceduresDetailsService proceduresDetailsService;

    @GetMapping("/ProductionScheduling/find")
    public DataResult find(
            @RequestParam(value = "workorderCode", required = false) String workorderCode,
            @RequestParam(value = "workorderName", required = false) String workorderName,
            @RequestParam(value = "sourceCode", required = false) String sourceCode,
            @RequestParam(value = "productCode", required = false) String productCode,
            @RequestParam(value = "productName", required = false) String productName,
            @RequestParam(value = "requestDate", required = false) String requestDate,
            @RequestParam(value = "clientCode", required = false) String clientCode,
            @RequestParam(value = "clientName", required = false) String clientName,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "currentPage", required = false) Integer currentPage
    ) {
        Page<ProWorkorder> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProWorkorder> queryWrapper = new QueryWrapper<>();
        if (workorderCode != null && !"".equals(workorderCode)) {
            queryWrapper.lambda().like(ProWorkorder::getWorkorderCode, workorderCode);
        }
        if (workorderName != null && !"".equals(workorderName)) {
            queryWrapper.lambda().like(ProWorkorder::getWorkorderName, workorderName);
        }
        if (sourceCode != null && !"".equals(sourceCode)) {
            queryWrapper.lambda().like(ProWorkorder::getSourceCode, sourceCode);
        }
        if (productCode != null && !"".equals(productCode)) {
            queryWrapper.lambda().like(ProWorkorder::getProductCode, productCode);
        }
        if (productName != null && !"".equals(productName)) {
            queryWrapper.lambda().like(ProWorkorder::getProductName, productName);
        }
        if (clientCode != null && !"".equals(clientCode)) {
            queryWrapper.lambda().like(ProWorkorder::getClientCode, clientCode);
        }
        if (clientName != null && !"".equals(clientName)) {
            queryWrapper.lambda().like(ProWorkorder::getClientName, clientName);
        }
        if (requestDate != null && !"".equals(requestDate)) {
            // 将 LocalDate 转换为包含当天开始和结束时间的 LocalDateTime
            LocalDateTime startDateTime = LocalDate.parse(requestDate).atTime(LocalTime.MIN);
            LocalDateTime endDateTime = LocalDate.parse(requestDate).atTime(LocalTime.MAX);
            queryWrapper.lambda().ge(ProWorkorder::getRequestDate, startDateTime);
            queryWrapper.lambda().le(ProWorkorder::getRequestDate, endDateTime);
        }

        //查找已确认订单的数据和自产品的数据
        queryWrapper.lambda().eq(ProWorkorder::getStatus, "CONFIRMED");
        queryWrapper.lambda().eq(ProWorkorder::getWorkorderType, "SELF");
        queryWrapper.orderByDesc("workorder_id");
        page = proWorkorderService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @GetMapping("/ProductionScheduling/Query")
    public DataResult Query(
            @RequestParam(value = "workorderCode", required = false) String workorderCode,
            @RequestParam(value = "processCode", required = false) String processCode
    ) {
        return DataResult.success(proTaskService.findDetails(workorderCode, processCode));
    }

    @GetMapping("/feedback/findProTask")
    public DataResult findProcess(@RequestParam(value = "workorderCode", required = false) String workorderCode,
                                  @RequestParam(value = "processCode", required = false) String processCode,
                                  @RequestParam(value = "workstationCode", required = false) String workstationCode,
                                  @RequestParam(value = "taskCode", required = false) String taskCode,
                                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                  @RequestParam(value = "currentPage", required = false) Integer currentPage
    ) {
        Page<ProTask> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProTask> queryWrapper = new QueryWrapper<>();
        if (workorderCode != null && !workorderCode.isEmpty()) {
            queryWrapper.lambda().eq(ProTask::getWorkorderCode, workorderCode);
        }
        if (processCode != null && !processCode.isEmpty()) {
            queryWrapper.lambda().eq(ProTask::getProcessCode, processCode);
        }
        if (workstationCode != null && !workstationCode.isEmpty()) {
            queryWrapper.lambda().eq(ProTask::getWorkstationCode, workstationCode);
        }
        if (taskCode != null && !taskCode.isEmpty()) {
            queryWrapper.lambda().eq(ProTask::getTaskCode, taskCode);
        }
        page = proTaskService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @GetMapping("/ProductionScheduling/findProcess")
    public DataResult findProcess(@RequestParam String productCode) {
        List<ProcedureDetails> procedureDetails = proceduresDetailsService.taskFindProcess(productCode);
        return DataResult.success(procedureDetails);
    }

    @PostMapping("/ProductionScheduling/insert")
    public DataResult add(@RequestBody ProTask proTask) {
        boolean save = proTaskService.save(proTask);
        return DataResult.success(save ? "成功" : "失败");
    }

    @PutMapping("/ProductionScheduling/update")
    public DataResult update(@RequestBody ProTask proTask) {
        boolean update = proTaskService.updateById(proTask);
        return DataResult.success(update ? "成功" : "失败");
    }

    @DeleteMapping("/ProductionScheduling/delete")
    public DataResult delete(@RequestBody List<String> id) {
        boolean remove = proTaskService.removeByIds(id);
        return DataResult.success(remove ? "成功" : "失败");
    }


}
